package com.ttbj.product.api.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.rpc.Result;
import com.ttbj.constant.GenerateNumConstants;
import com.ttbj.core.util.DtoTransitionUtils;
import com.ttbj.core.util.RegexUtil;
import com.ttbj.driver.bean.po.Driver;
import com.ttbj.driver.dao.DriverMapper;
import com.ttbj.driver_bo.dao.DriverBoMapper;
import com.ttbj.product.api.DriverFacade;
import com.ttbj.product.api.LocationFacade;
import com.ttbj.product.bean.dto.DriverDto;
import com.ttbj.product.bean.dto.EntitieDto;
import com.ttbj.product.bean.dto.TruckDto;
import com.ttbj.truck.bean.po.Truck;
import com.ttbj.truck.dao.TruckMapper;
import com.ttbj.util.Assert;
import com.ttbj.util.DateUtils;
import com.ttbj.util.ResultUtil;
import com.ttbj.util.TokenUtils;
import com.ttbj.util.ViewModelUtil;

import cn.jiguang.common.utils.StringUtils;

@Service("driverFacade")
public class DriverFacadeImpl implements DriverFacade {
	private Logger logger = LoggerFactory.getLogger(DriverFacadeImpl.class);

	@Autowired
	private DriverMapper driverMapper = null;

	@Autowired
	private TruckMapper truckMapper = null;

	@Autowired
	private LocationFacade locationFacade = null;
	
	@Autowired
	private DriverBoMapper driverBoMapper = null;

	@Override
	public DriverDto findDriver(DriverDto driverDto, int sen) {
		if (null == driverDto) {
			logger.info("出现问题：提供的司机查询参数driverDto为null");
			return null;
		}
		return driverMapper.getDriver((Driver) DtoTransitionUtils.trans(Driver.class, driverDto), sen);
	}

	@Override
	public void newDriver(DriverDto driverDto) throws Exception {
		/* 获取entityName */
		String entityName = locationFacade.entityAdd(driverDto.getDriverRealName(), null, null, null);
		logger.info("获取到的entity:{}", entityName);
		if (StringUtils.isEmpty(entityName)) {
			ResultUtil.throwExcepion(ViewModelUtil.buildFailure(99999, "系统未知错误"), "创建鹰眼entity时出错");
		}
		Driver driver =(Driver) DtoTransitionUtils.trans(Driver.class, driverDto.entity(entityName));
		Assert.assertNonNullObj(driver, 503);
		if (0 >= driverMapper.insertSelective(driver)) {
			ResultUtil.throwExcepion(ViewModelUtil.buildFailure(99999, "系统未知错误"), "添加司机用户信息时出错");
		}
	}

	@Override
	public void updateDriver(DriverDto driverDto) throws Exception {
		int code = driverMapper.updateByPrimaryKeySelective((Driver) DtoTransitionUtils.trans(Driver.class, driverDto));
		if (code <= 0) {
			ResultUtil.throwExcepion(ViewModelUtil.buildFailure(99999, "系统未知错误"), "修改司机用户信息时出错");
		}
	}

	@Override
	public Long newTruck(Long driverId,String carNum, String truckType) throws Exception {
		if(!RegexUtil.isCarnumberNO(carNum)){
			ResultUtil.throwExcepion(ResultUtil.createFail("messages", 911, "车牌号"), "车牌号不符合规范,车牌号");
		}
		Assert.assertNumeric(truckType, 421);
		Integer type = Integer.valueOf(truckType);
		Truck truck = new Truck(TokenUtils.getNo(GenerateNumConstants.TRUCK), carNum, type, DateUtils.getCurDttStr(), driverId);
		truckMapper.insertSelective(truck);
		return truck.getTruckId();
	}

	@Override
	public List<TruckDto> likeTruck(String carNum) throws Exception {
		return truckMapper.likeTruck(carNum);
	}

	@Override
	public boolean isContainEntity(DriverDto driver) {
		if (null != driver) {
			String entity = driver.getDriverEntity();
			if (!StringUtils.isEmpty(entity)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public List<DriverDto> matchEntityWithTruckType(Integer truckType, List<EntitieDto> list) {
		if(null == truckType || Assert.isEmpty(list))
			return null;
		return driverBoMapper.matchEntityWithTruckType(truckType, list);
	}

	@Override
	public String entityAdd(String name, String desc, String city, String district) throws Exception {
		return locationFacade.entityAdd(name, desc, city, district);
	}

	@Override
	public int updateTruck(TruckDto truck) {
		if( null == truck){
			return 0;
		}
		return truckMapper.updateByPrimaryKeySelective((Truck)DtoTransitionUtils.trans(Truck.class, truck));
		
	}

	@Override
	public TruckDto getTruck(Long truckId) {
		Truck truck = truckMapper.selectByPrimaryKey(truckId);
		if(null != truck){
			return (TruckDto)DtoTransitionUtils.trans(TruckDto.class, truck);
		}
		return null;
	}

	@Override
	public DriverDto queryDto(Long driverId) {
		return driverBoMapper.queryDriver(driverId);
	}

	@Override
	public TruckDto queryTruckDto(TruckDto truckDto) {
		if( null == truckDto){
			return null;
		}
		return 	truckMapper.queryDto(truckDto);
	}

	@Override
	public void logout(DriverDto driverDto) {
		driverMapper.logout(driverDto);
	}

}
